// 获取URL上的参数。返回对象或者值。
export const getUrlParam = (
  obj = {
    url: "",
    key: "",
  }
) => {
  if (!obj) return "";
  const { url = "", key = "" } = obj;
  if (!url) return "";
  const u = new URL(url),
    searchParams = new URLSearchParams(u.search);
  return searchParams.get(key) ?? "";
};

// 时间个位数变两位数。
// 输入1-2位数，输出2位数的字符串。
export const oneBecomesTwo = (num) => {
  if (typeof num === "number") {
    return num < 10 ? `0${num}` : `${num}`;
  } else {
    try {
      const newNum = Number(num);
      if (isNaN(newNum)) return num;
      return newNum < 10 ? `0${newNum}` : `${newNum}`;
      // eslint-disable-next-line no-empty
    } catch {}
  }
};
